package com.zhaotianhao.service.impl;

import com.zhaotianhao.pojo.Users;
import com.zhaotianhao.mapper.UsersMapper;
import com.zhaotianhao.service.UsersService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * (Users)表服务实现类
 *
 * @author Z-7
 * @since 2022-03-01 10:10:44
 */
@DubboService
public class UsersServiceImpl implements UsersService {
    @Autowired
    private UsersMapper usersMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Users queryById(Integer id) {
        return this.usersMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param users 筛选条件
     * @param pageNum 起始页数
     * @param pageSize 分页偏移量
     * @return 查询结果
     */
    @Override
    public PageInfo<Users> queryByPage(Users users,int pageNum,int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(this.usersMapper.queryAll(users));
    }

    /**
     * 新增数据
     *
     * @param users 实例对象
     * @return 影响行数
     */
    @Override
    public int insert(Users users) {
        return this.usersMapper.insert(users);
    }

    /**
     * 修改数据
     *
     * @param users 实例对象
     * @return 影响行数
     */
    @Override
    public int update(Users users) {
        return this.usersMapper.update(users);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.usersMapper.deleteById(id) > 0;
    }

    /**
     * 登录
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public Users login(String username, String password) {
        Users users = this.usersMapper.queryByName(username);
        if(users == null ){
            return null;
        }
        String s = DigestUtils.md5Hex(password);
        System.out.println(s);
        if(users.getPassword().equals(s)){
            System.out.println("登录成功");
            return users;
        }
        System.out.println("登录失败");
        return null;
    }
}
